package com.jajb.controldeiluminacion;

import android.app.Activity;
import android.os.Message;


public class SerialDataHelper extends Activity {
    static private byte[] readBuffer;
    static private String strIncom;
    static private StringBuilder sb = new StringBuilder();
    static public boolean isFullMessage;
    static public String receivedMessage;


    // Metodo para leer los datos del mensaje
    static void ReadData(Message message) {
        readBuffer = (byte[]) message.obj;
        strIncom = new String(readBuffer, 0, message.arg1);
        sb.append(strIncom);

        GetFullMessage(sb);
    }

    // Metodo para obtener un mensaje completo
    static private void GetFullMessage(StringBuilder sb) {

        int startOfLineIndex = sb.indexOf("<");
        int endOfLineIndex = sb.indexOf(">", startOfLineIndex);

        if (startOfLineIndex >= 0 && endOfLineIndex > startOfLineIndex) {
            receivedMessage = sb.substring(startOfLineIndex, endOfLineIndex + 1);
            isFullMessage = true;
            sb.delete(0, sb.length());
        } else {
            receivedMessage = "";
            isFullMessage = false;
        }
    }


    // Metodo para obtener el valor de un parametro
    static public String GetValue(char param) {

        if (isFullMessage)
            return receivedMessage.substring(receivedMessage.indexOf(param) + 1, receivedMessage.indexOf("@", receivedMessage.indexOf(param)));
        else
            return "";
    }
}
